package com.ccjq.jq173.shosestore.web.controller.product;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

import com.ccjq.jq173.shosestore.core.util.PageBean;
import com.ccjq.jq173.shosestore.core.util.PageHelper;
import com.ccjq.jq173.shosestore.member.po.User;
import com.ccjq.jq173.shosestore.product.po.Category;
import com.ccjq.jq173.shosestore.product.po.Product;
import com.ccjq.jq173.shosestore.product.service.*;
import com.ccjq.jq173.shosestore.product.service.impl.productServiceImpl;
import com.ccjq.jq173.shosestore.shoppingcart.service.ShoppingcartService;
import com.ccjq.jq173.shosestore.shoppingcart.service.impl.ShoppingcartServiceImpl;


public class ProductServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String flag=request.getParameter("flag");
        productService p = new productServiceImpl();
        if(request.getSession().getAttribute("ys")==null)
        {
            request.getSession().setAttribute("ys","6");
        }
        if("product".equals(flag))
        {
            List<Category> list=p.getPinPai();
            request.getSession().setAttribute("list",list);
            List<Product> list2=p.getHotShoes();
            request.getSession().setAttribute("list2",list2);
            int count=p.getCount();
            String ys=(String)request.getSession().getAttribute("ys");
            String pageSize=request.getParameter("ps")==null?ys:request.getParameter("ps");

            PageBean pageBean=PageHelper.getPager(request,count,Integer.parseInt(pageSize));
            List<Product> list5=p.getAllShoes(pageBean.getStartRow(),pageBean.getPageSize());
            request.getSession().setAttribute("ys",pageSize);
            request.setAttribute("pageBean",pageBean);
            request.setAttribute("fenYepd","all");
            request.getSession().setAttribute("list5",list5);
            request.getRequestDispatcher("/WEB-INF/jsp/product/product_list.jsp").forward(request,response);
        }
        else if("xq".equals(flag)){
            String productId=request.getParameter("productId");

            Product p6=p.getOneShoes(productId);
            String categoryId=p6.getCategoryId();
            String name=p.getPinPai(categoryId);
            p6.setName(name);
            p6.setProductId(productId);
            request.getSession().setAttribute("p6",p6);
            request.getRequestDispatcher("/WEB-INF/jsp/product/produc_detail.jsp").forward(request,response);
        }
        else if("gwc".equals(flag)){
            String productId=request.getParameter("productId");
            User user=((User)request.getSession().getAttribute("user"));
            System.out.println("user"+user);
            System.out.println(productId);
            if(user==null)
            {
                request.getRequestDispatcher("/WEB-INF/jsp/member/login.jsp").forward(request,response);
                return;
            }
            System.out.println("userid:"+user.getUserId());
            String sl=request.getParameter("sl");
            System.out.println(sl);
            ShoppingcartService shop=new ShoppingcartServiceImpl();
            boolean b=shop.addCar(productId,user.getUserId(),Integer.parseInt(sl));
            System.out.println(b);
            request.getRequestDispatcher("shopcar?tz=gocar").forward(request,response);
        }
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
